package cn.iocoder.yudao.module.system.dal.redis.dict.dao;

import cn.iocoder.yudao.module.system.dal.redis.RedisKeyConstants;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.time.Duration;

/**
 * @Author cjc
 * @Date 2024/5/20 16:54
 * @Description: 字典值redis操作
 * @Version 1.0
 */
@Repository
public class DictRedisDao {


    @Resource
    private StringRedisTemplate stringRedisTemplate;

    public String get(String dictType,String dictValue) {
        String redisKey = formatKey(dictType,dictValue);
        return stringRedisTemplate.opsForValue().get(redisKey);
    }

    public void set(String dictType, String dictValue,String dictLabel, Duration timeout) {
        String redisKey = formatKey(dictType,dictValue);
        stringRedisTemplate.opsForValue().set(redisKey, dictLabel, timeout);
    }

    public void delete(String dictType,String dictValue) {
        String redisKey = formatKey(dictType,dictValue);
        stringRedisTemplate.delete(redisKey);
    }

    private static String formatKey(String dictType,String dictValue) {
        return String.format(RedisKeyConstants.DICT_LABEL_TEMPLATE, dictType,dictValue);
    }


}
